package br.com.sysprova.view;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import br.com.sysprova.controller.UsuarioController;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaCadastroProfessor {

	private UsuarioController usuarioController;

	private JFrame frame;
	private JTextField txtNomeCadProf;
	private JPasswordField txtRepSenhaCadProf;
	private JPasswordField txtSenhaCadProf;
	private JTextField txtMatCadProf;
	private JTextField txtEmailCadProf;

	private JButton btnConfirmarCadProf;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaCadastroProfessor window = new TelaCadastroProfessor();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public TelaCadastroProfessor() {
		initialize();
		iniciaUsuarioControlle();
	}

	private void iniciaUsuarioControlle() {
		if (usuarioController == null) {
			usuarioController = new UsuarioController();
		}

	}

	public Point centralizarFrame() {
		Rectangle r = frame.getBounds();
		// Dimensoes da janela
		int widthSplash = r.width;
		int heightSplash = r.height;

		return (new Point(((int) Toolkit.getDefaultToolkit().getScreenSize()
				.getWidth() / 2)
				- (widthSplash / 2), ((int) Toolkit.getDefaultToolkit()
				.getScreenSize().getHeight() / 2)
				- (heightSplash / 2)));
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setTitle("SysProva");
		frame.setAlwaysOnTop(true);
		frame.setLocation(centralizarFrame());
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setVisible(true);

		JLabel lblCadProf = new JLabel("Cadastro de Professores");
		lblCadProf.setFont(new Font("SketchFlow Print", Font.BOLD, 18));
		lblCadProf.setBounds(84, 11, 273, 33);
		frame.getContentPane().add(lblCadProf);

		JLabel lblNomeCabProf = new JLabel("Nome");
		lblNomeCabProf.setBounds(28, 77, 46, 14);
		frame.getContentPane().add(lblNomeCabProf);

		txtNomeCadProf = new JTextField();
		txtNomeCadProf.setColumns(10);
		txtNomeCadProf.setBounds(118, 74, 254, 20);
		frame.getContentPane().add(txtNomeCadProf);

		JLabel lblEmailCadProf = new JLabel("E-mail");
		lblEmailCadProf.setBounds(28, 102, 46, 14);
		frame.getContentPane().add(lblEmailCadProf);

		JLabel lblMatCadProf = new JLabel("Matr\u00EDcula");
		lblMatCadProf.setBounds(28, 127, 60, 14);
		frame.getContentPane().add(lblMatCadProf);

		JLabel lblSenhaCadProf = new JLabel("Senha");
		lblSenhaCadProf.setBounds(28, 152, 46, 14);
		frame.getContentPane().add(lblSenhaCadProf);

		JLabel lblRepSenhaCadProf = new JLabel("Repetir Senha");
		lblRepSenhaCadProf.setBounds(28, 177, 80, 14);
		frame.getContentPane().add(lblRepSenhaCadProf);

		txtRepSenhaCadProf = new JPasswordField();
		txtRepSenhaCadProf.setBounds(118, 174, 254, 20);
		frame.getContentPane().add(txtRepSenhaCadProf);

		txtSenhaCadProf = new JPasswordField();
		txtSenhaCadProf.setBounds(118, 149, 254, 20);
		frame.getContentPane().add(txtSenhaCadProf);

		txtMatCadProf = new JTextField();
		txtMatCadProf.setColumns(10);
		txtMatCadProf.setBounds(118, 124, 254, 20);
		frame.getContentPane().add(txtMatCadProf);

		txtEmailCadProf = new JTextField();
		txtEmailCadProf.setColumns(10);
		txtEmailCadProf.setBounds(118, 99, 254, 20);
		frame.getContentPane().add(txtEmailCadProf);

		btnConfirmarCadProf = new JButton("Confirmar");
		eventoBotaoConfirmar();
		btnConfirmarCadProf.setBounds(151, 213, 127, 23);
		frame.getContentPane().add(btnConfirmarCadProf);
	}

	private void eventoBotaoConfirmar() {
		btnConfirmarCadProf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nome = txtNomeCadProf.getText();
				String matricula = txtMatCadProf.getText();
				String email = txtEmailCadProf.getText();
				String senha = String.valueOf(txtSenhaCadProf.getPassword());
				String repSenha = String.valueOf(txtRepSenhaCadProf
						.getPassword());
				System.out.println(senha + "  " + repSenha);
				if (validaSenha(senha, repSenha)) {
//					usuarioController.salvar(email, matricula, nome, senha, 40, "AiDento");
					if (usuarioController.salvarProfessor(email, matricula,
							nome, senha, 40, "Sr")) {
						JOptionPane.showMessageDialog(null,
								"Professor Cadastrado");
					}

				} else {
					JOptionPane.showMessageDialog(null, "Senhas incompatíveis");
				}
			}

		});

	}

	private boolean validaSenha(String senha, String repSenha) {
		if (senha.trim().equals(repSenha.trim())) {
			return true;
		}
		return false;

	}
}
